/*
 * FitnessFunctionFactory.java
 *
 * Created on den 30 mars 2007, 23:47
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package grex.fitnessfunctions;

import grex.BNFException;
import grex.Options;
import grex.genes.GeneException;

import java.io.Serializable;

/**
 *
 * @author rik
 */
public class FitnessFunctionFactory implements Serializable{
    private FitnessFunction fitnessFunction;
    private FitnessFunction simplificationFunction; 
    private String currrentFitnessFunction,currentSimplificationFunction;
    private Options options;
    /** Creates a new instance of FitnessFunctionFactory */
    public FitnessFunctionFactory(Options options) throws BNFException{
        this.options=options;
        try{
            String path="grex.fitnessfunctions.";
            if(options.getPROBLEM_TYPE()==Options.CLASSIFICATION)
                path += "Classification.";
            else
                path +="Regression.";

        fitnessFunction=(FitnessFunction)( Class.forName( path + options.getFITNESS_FUNCTION() ).newInstance());
        simplificationFunction=(FitnessFunction)( Class.forName(path + options.getSIMPLIFICATION_FUNCTION() ).newInstance());
        currrentFitnessFunction = options.getFITNESS_FUNCTION();
        currentSimplificationFunction = options.getSIMPLIFICATION_FUNCTION();
        } catch(Exception e){
            throw new BNFException("Fitness function could not be found\n" +e);
        }
    }
    public void clearFitnessFunction() throws GeneException{
        currrentFitnessFunction = "Not initsialized";
    }
    
    public FitnessFunction getFitnessFunction() throws GeneException {
        try {
          
            if(options.getFITNESS_FUNCTION().equalsIgnoreCase(currrentFitnessFunction) && !options.isRUN_SIMPLIFICATION())
                return fitnessFunction;
            
            
            String path="grex.fitnessfunctions.";
            if(options.getPROBLEM_TYPE()==Options.CLASSIFICATION)
                path += "Classification.";
            else
                path +="Regression.";
            
            
            if (options.isRUN_SIMPLIFICATION()) {
                if(!options.getSIMPLIFICATION_FUNCTION().equalsIgnoreCase(currentSimplificationFunction)){
                  simplificationFunction=(FitnessFunction)( Class.forName(path + options.getSIMPLIFICATION_FUNCTION() ).newInstance());
                  currentSimplificationFunction = options.getSIMPLIFICATION_FUNCTION();
                }
                return simplificationFunction;
            } else if (!options.getFITNESS_FUNCTION().equalsIgnoreCase(currrentFitnessFunction)) {
                fitnessFunction = (FitnessFunction) ( Class.forName( path + options.getFITNESS_FUNCTION() ).newInstance());
                currrentFitnessFunction = options.getFITNESS_FUNCTION();
            }                 
            return fitnessFunction;
        } catch (Exception ex) {
            throw new GeneException(ex.getMessage());
        }
    }    
}
